package com.moxiao.yfjh.module.order.controller.admin.payRecord;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.order.controller.admin.payRecord.vo.*;
import com.moxiao.yfjh.module.order.convert.payRecord.PayRecordConvert;
import com.moxiao.yfjh.module.order.dal.dataobject.payRecord.PayRecordDO;
import com.moxiao.yfjh.module.order.tradeRefund.payRecord.PayRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 支付记录")
@RestController
@RequestMapping("/order/pay-record")
@Validated
public class PayRecordController {

    @Resource
    private PayRecordService payRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建支付记录")
    @PreAuthorize("@ss.hasPermission('order:pay-record:create')")
    public CommonResult<Long> createPayRecord(@Valid @RequestBody PayRecordCreateReqVO createReqVO) {
        return success(payRecordService.createPayRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新支付记录")
    @PreAuthorize("@ss.hasPermission('order:pay-record:update')")
    public CommonResult<Boolean> updatePayRecord(@Valid @RequestBody PayRecordUpdateReqVO updateReqVO) {
        payRecordService.updatePayRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除支付记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('order:pay-record:delete')")
    public CommonResult<Boolean> deletePayRecord(@RequestParam("id") Long id) {
        payRecordService.deletePayRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得支付记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('order:pay-record:query')")
    public CommonResult<PayRecordRespVO> getPayRecord(@RequestParam("id") Long id) {
        PayRecordDO payRecord = payRecordService.getPayRecord(id);
        return success(PayRecordConvert.INSTANCE.convert(payRecord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得支付记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('order:pay-record:query')")
    public CommonResult<List<PayRecordRespVO>> getPayRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<PayRecordDO> list = payRecordService.getPayRecordList(ids);
        return success(PayRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得支付记录分页")
    @PreAuthorize("@ss.hasPermission('order:pay-record:query')")
    public CommonResult<PageResult<PayRecordRespVO>> getPayRecordPage(@Valid PayRecordPageReqVO pageVO) {
        PageResult<PayRecordDO> pageResult = payRecordService.getPayRecordPage(pageVO);
        return success(PayRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出支付记录 Excel")
    @PreAuthorize("@ss.hasPermission('order:pay-record:export')")
    @OperateLog(type = EXPORT)
    public void exportPayRecordExcel(@Valid PayRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PayRecordDO> list = payRecordService.getPayRecordList(exportReqVO);
        // 导出 Excel
        List<PayRecordExcelVO> datas = PayRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "支付记录.xls", "数据", PayRecordExcelVO.class, datas);
    }

}
